package com.youlai.boot.system.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.system.model.QB.Device;
import com.youlai.boot.system.model.QB.Plot;
import com.youlai.boot.system.service.PlotService;
import com.youlai.boot.system.service.impl.DeviceServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/plot")
public class PlotController {
    @Autowired
    private PlotService plotService;
    private static final Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
    @PostMapping("/add")
    public Result<?> addPlot(@RequestBody Plot plot){
        return Result.judge(plotService.addPlot(plot),"新增设备成功","新增设备失败") ;
    }
    @PostMapping("/all")
    public Result<List<Plot>> getAllPlots(){
        List<Plot> plots = plotService.getAllPlots();
        Result<List<Plot>> result;
        if (!plots.isEmpty()){
            result  = Result.success(plots);
            result.setMessage("查询地块成功");
            result.setCode(ResultCode.SUCCESS.getCode());
        }else {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
            result.setMessage("查询设备失败");
        }
        return result;
    }
    @GetMapping("/{plotnumber}/number")
    public Result<Plot> getPlotByNumber(@PathVariable("plotnumber") String plotnumber){
        Plot plot = plotService.getPlotByNumber(plotnumber);
        if (plot !=null){
            return Result.success(plot);
        }
        else {
            return Result.failed("未找到相关编号" +plotnumber + "的设备");
        }
    }
    @PostMapping("/{plotnumber}/stasus")
    public  Result<?> updatePlotStatus(@PathVariable("plotnumber") String plotnumber,@RequestParam  int newStatus){
        Result result = new Result();
        boolean success = plotService.updatePlotStatus(plotnumber,newStatus);
        if (success){
            result.setSuccess(true);
            result.setMessage("设备状态修改成功");
        }else {
            result.setSuccess(false);
            result.setMessage("设备状态修改失败");
        }
        return result;
    }



    @DeleteMapping("/{id}")
    public  Result<?> deleteHygiene( @PathVariable Integer id){
        return Result.judge(plotService.deletePlotById(id),"删除地块成功","删除地块失败") ;
    }
    @PutMapping("/upd")
    public  Result<?> updateDevice(@RequestBody Plot device){
        return Result.judge(plotService.updateDevice(device),"修改成功","修改失败");
    }
}
